package org.cs.controlmachine;

import org.cs.program.Instruction;

/**
 * 控制器
 *
 * @author fbin
 * @since 2022/4/5 11:20
 */
public class ControlMachine {
    private static final ControlUnit CU;
    private static final InstructionRegister IR;
    private static final ProgramCounter PC;

    static {
        CU = new ControlUnit();
        IR = new InstructionRegister();
        PC = new ProgramCounter();
    }

    /**
     * 获取当前要执行的指令地址
     *
     * @return 指令地址
     */
    public int getCurrentInstructionAddress() {
        return PC.getNextInstructionAddress();
    }

    /**
     * 解析指令
     *
     * @param binaryInstruction 二进制指令
     */
    public void analysisInstruction(Object binaryInstruction) {
        Instruction instruction = CU.analysis(binaryInstruction);
        IR.setInstruction(instruction);
    }

    /**
     * 获取当前指令
     *
     * @return 指令
     */
    public Instruction getCurrentInstruction() {
        return IR.getInstruction();
    }
}
